package org.jeecg.modules.demo.zhouqibiao.controller;

import java.util.*;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.query.QueryRuleEnum;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.Shenhefankuibiao.entity.Shenhefankuibiao;
import org.jeecg.modules.demo.Shenhefankuibiao.service.IShenhefankuibiaoService;
import org.jeecg.modules.demo.peihebumenbiao.service.ShenheDate;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.entity.Zhidingzhouqigongzuo;
import org.jeecg.modules.demo.zhidingzhouqigongzuo.service.IZhidingzhouqigongzuoService;
import org.jeecg.modules.demo.zhouqibiao.entity.ShenheDto;
import org.jeecg.modules.demo.zhouqibiao.entity.Zhouqibiao;
import org.jeecg.modules.demo.zhouqibiao.service.IZhouqibiaoService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 周期表
 * @Author: jeecg-boot
 * @Date:   2025-10-09
 * @Version: V1.0
 */
@Tag(name="周期表")
@RestController
@RequestMapping("/zhouqibiao/zhouqibiao")
@Slf4j
public class ZhouqibiaoController extends JeecgController<Zhouqibiao, IZhouqibiaoService> {
	@Autowired
	private IZhouqibiaoService zhouqibiaoService;
	@Autowired
	private IShenhefankuibiaoService shenhefankuibiaoService;
	@Autowired
	private IZhidingzhouqigongzuoService zhidingzhouqigongzuoService;
	/**
	 * 分页列表查询
	 *
	 * @param zhouqibiao
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "周期表-分页列表查询")
//	@Operation(summary="周期表-分页列表查询")
//	@GetMapping(value = "/list")
//	public Result<IPage<Zhouqibiao>> queryPageList(Zhouqibiao zhouqibiao,
//								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
//								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
//								   HttpServletRequest req) {
//
//
//        QueryWrapper<Zhouqibiao> queryWrapper = QueryGenerator.initQueryWrapper(zhouqibiao, req.getParameterMap());
//		Page<Zhouqibiao> page = new Page<Zhouqibiao>(pageNo, pageSize);
//		IPage<Zhouqibiao> pageList = zhouqibiaoService.page(page, queryWrapper);
//		return Result.OK(pageList);
//	}
	//对于责任主管，该页面展示自己部门负责的且各配合部门都以完成且是本周期的任务，确认无误点击审核通过
	 @GetMapping(value = "/list")
	 public Result<IPage<ShenheDto>> queryPageList(Zhouqibiao zhouqibiao,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		//先查出所有进行中的任务，获取指标id，根据指标id以及完成时间查出各配合部门完成情况，如果全部完成，则添加到列表中，展示给该部长
		 Page<Zhouqibiao> page = new Page<Zhouqibiao>(pageNo, pageSize);
		 QueryWrapper<Zhouqibiao> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("wanchengqingkuang","进行中");
		 IPage<Zhouqibiao> pageList = zhouqibiaoService.page(page, queryWrapper);
		 List<Zhouqibiao> list = pageList.getRecords();
		 IPage<ShenheDto> pageList1 = new Page<>();
		 List<Zhouqibiao> list1 = new ArrayList<>();
		 for (int i = 0; i < list.size(); i++) {
			 QueryWrapper<Shenhefankuibiao> qw = new QueryWrapper<>();
			 qw.eq("zhidingbiaoid",list.get(i).getZhidingbiaoid());
			 qw.eq("wanchengshijian",list.get(i).getWanchengshijian());
			 qw.eq("zhuangtai","进行中");
			 List<Shenhefankuibiao> list2 = shenhefankuibiaoService.list(qw);
			 if(list2.size()==0){
				 list1.add(list.get(i));
			 }else{
				 System.out.println("暂无数据");
			 }
		 }
		 List<ShenheDto> list3 = new ArrayList<>();
		 for(int j=0;j<list1.size();j++){
			 ShenheDto shenheDto = new ShenheDto();
			 shenheDto.setId(list1.get(j).getId());
			 QueryWrapper<Zhidingzhouqigongzuo> queryWrapper2 = new QueryWrapper<>();
			 queryWrapper2.eq("id",list1.get(j).getZhidingbiaoid());
			 Zhidingzhouqigongzuo zhidingzhouqigongzuo = zhidingzhouqigongzuoService.getOne(queryWrapper2);
			 shenheDto.setGongzuoneirong(zhidingzhouqigongzuo.getGongzuoneirong());
			 shenheDto.setZhiliangyaoqiu(zhidingzhouqigongzuo.getZhiliangyaoqiu());
			 shenheDto.setWanchenshijian(list1.get(j).getWanchengshijian());
			 shenheDto.setZerenzhuguanid(zhidingzhouqigongzuo.getZerenzhuguanid());
			 list3.add(shenheDto);
		 }
		 pageList1.setRecords(list3);
		 return Result.OK(pageList1);
	}

	//部长审核
	 @PostMapping(value = "/shenhe")
	 public Result<String> shenhe(@RequestBody ShenheDto shenheDto) {
		String id = shenheDto.getId();
		Zhouqibiao zhouqibiao = zhouqibiaoService.getById(id);
		zhouqibiao.setWanchengqingkuang(shenheDto.getCaozuo());
		 System.out.println("操作："+shenheDto.getCaozuo());
		zhouqibiaoService.updateById(zhouqibiao);
		return Result.OK("审核成功！");
	}
	/**
	 *   添加
	 *
	 * @param zhouqibiao
	 * @return
	 */
	@AutoLog(value = "周期表-添加")
	@Operation(summary="周期表-添加")
	@RequiresPermissions("zhouqibiao:y_zhouqibiao:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Zhouqibiao zhouqibiao) {
		zhouqibiaoService.save(zhouqibiao);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zhouqibiao
	 * @return
	 */
	@AutoLog(value = "周期表-编辑")
	@Operation(summary="周期表-编辑")
	@RequiresPermissions("zhouqibiao:y_zhouqibiao:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Zhouqibiao zhouqibiao) {
		zhouqibiaoService.updateById(zhouqibiao);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "周期表-通过id删除")
	@Operation(summary="周期表-通过id删除")
	@RequiresPermissions("zhouqibiao:y_zhouqibiao:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zhouqibiaoService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "周期表-批量删除")
	@Operation(summary="周期表-批量删除")
	@RequiresPermissions("zhouqibiao:y_zhouqibiao:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zhouqibiaoService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "周期表-通过id查询")
	@Operation(summary="周期表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Zhouqibiao> queryById(@RequestParam(name="id",required=true) String id) {
		Zhouqibiao zhouqibiao = zhouqibiaoService.getById(id);
		if(zhouqibiao==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zhouqibiao);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zhouqibiao
    */
    @RequiresPermissions("zhouqibiao:y_zhouqibiao:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Zhouqibiao zhouqibiao) {
        return super.exportXls(request, zhouqibiao, Zhouqibiao.class, "周期表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("zhouqibiao:y_zhouqibiao:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Zhouqibiao.class);
    }

}
